<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="card-title">
        <span>我的投资</span>
      </div>
      <div class="investRecord-box">
        <!-- 日期选择器容器 -->
        <div class="date-box">
          <span class="date-span">日期选择</span>
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd"
            @change="onDateChange"
          >
          </el-date-picker>
          <el-button type="success" class="filter-btn" @click="onSearch"
            >查询</el-button
          >
        </div>
        <div class="investRecordList-box">
          <el-tabs type="border-card" @tab-click="onTabsClick">
            <el-tab-pane
              v-for="(item, index) in tabsList"
              :key="index"
              :label="item"
            >
              <el-table
                :data="tableData"
                stripe
                style="width: 100%"
                :header-cell-style="{ background: '#e1e4e5' }"
              >
                <el-table-column prop="plan_name" label="计划名称">
                </el-table-column>
                <el-table-column prop="pId" label="投资编号">
                </el-table-column>
                <el-table-column prop="invest_amount" label="投资金额(元)"> </el-table-column>
                <el-table-column prop="totalIncome" label="总代收利息(元)">
                </el-table-column
                ><el-table-column prop="yearRate" label="年利率(%)">
                </el-table-column>
                <el-table-column prop="period" label="期数">
                </el-table-column>
                <el-table-column prop="deal_date" label="购买时间">
                </el-table-column>
                <el-table-column prop="remark" label="备注">
                </el-table-column>            
              </el-table></el-tab-pane
            >
          </el-tabs>
        </div>
        <el-pagination
          @current-change="onPageChange"
          background
          layout="prev, pager, next"
          :page-count="filterData.totalPages"
          :current-page="filterData.curPage"
          :page-size="filterData.perPage"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>
  
  <script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
      filterData: {
        // 日期开始时间
        start: "",
        // 日期结束时间
        end: "",
        // 总页数
        totalPages: 1,
        // 当前页
        curPage: 1,
        // 每页显示几条数据
        per_page: 3,
        // 投资记录的状态
        invest_type: 0,
      },
      tabsList: ["购买中的计划", "回款中的计划",'已完成的计划'],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      dateRange: "",
      tableData: [],
    };
  },
  methods: {
    ...mapActions({
      getInvestRecord: "invest/getInvestRecord",
    }),
    getInvestRecordList() {
      this.getInvestRecord(this.filterData).then((res) => {
        this.tableData = res.content;
        this.filterData.totalPages = res.paginateInfo.pages;
      });
    },
    // 点击查询按钮
    onSearch() {
      this.filterData.curPage = 1;
      this.getInvestRecordList();
    },
    // 当前页码发生变化时的回调
    onPageChange(index) {
      this.filterData.curPage = index;
      this.getInvestRecordList();
    },
    // 当日期选择器选择的内容发生改变
    onDateChange(data) {
      if (data) {
        this.filterData.start = data[0];
        this.filterData.end = data[1];
      } else {
        this.filterData.start = "";
        this.filterData.end = "";
      }
    },
    // tab 被选中时触发
    onTabsClick(data) {
      this.filterData.invest_type = data.index;
      this.filterData.curPage = 1;
      this.getInvestRecordList();
    },
  },
  created() {
    this.getInvestRecordList();
  },
};
</script>
  
  <style lang="scss" scoped>
.app-container {
  .investRecord-box {
    .date-span {
      margin-right: 20px;
    }
    .filter-btn {
      margin-left: 10px;
    }
  }
  .investRecordList-box {
    margin-top: 15px;
  }
}
</style>